from typing import Annotated

from fastapi import Query, Body
from pydantic import model_validator, BaseModel

from app.utils import types
from app.models.common import Pagination, SystemBoardId
from app.models import responses


class SystemBoardsList(Pagination):
    """分页获取系统公告"""
    pass


SystemBoardsListParams = Annotated[SystemBoardsList, Query()]


class CreateSystemBoard(BaseModel):
    content: str

    @model_validator(mode='before')
    @classmethod
    def before(cls, data):
        data = types.safe_dict(data)
        data['content'] = str(data.get('content', ''))
        return data

    @model_validator(mode='after')
    def after(self):
        if len(self.content) <= 0:
            raise responses.FailureResponseError(-102, msg='公告内容不能为空')

        return self


CreateSystemBoardJSON = Annotated[CreateSystemBoard, Body()]

DeleteSystemBoardJSON = Annotated[SystemBoardId, Body()]


class BatchSystemBoardIds(BaseModel):
    board_ids: list[int] = []

    @model_validator(mode='before')
    @classmethod
    def before(cls, data):
        data = types.safe_dict(data)
        board_ids = []
        for raw_board_id in list(data.get('board_ids', [])):
            board_id = types.safe_int(raw_board_id, None)
            if board_id is not None:
                board_ids.append(board_id)

        data['board_ids'] = board_ids
        return data

    @model_validator(mode='after')
    def after(self):
        if type(self.board_ids) is not list:
            raise responses.FailureResponseError(-102, msg='多个公告 ID 必须是列表类型')

        if len(self.board_ids) == 0:
            raise responses.FailureResponseError(-102, msg='请传入至少 1 个公告 ID')

        return self


BatchDeleteBoardsJSON = Annotated[BatchSystemBoardIds, Body()]
